﻿log("Running Taobao Browser 2(Sub-Items Searcher 2)  ...  searchTaobao2.js")

// current position
isIndexPage = function ()
{
    return false;
}
isSearchPage = function ()
{
    return location.href.search("search.htm") != -1;
}

// index
getSearchEdit = null;
getSubmitButton = null;

// search methods
priceSetted = null;
getSortMethod = null;
locationSetted = null;

// price 
getItemLowerPriceEdit = null;
getItemHigherPriceEdit = null;
getSubmitPriceButton = null;

// sort by
getSortByDefaultButton = null;
getSortBySalesButton = null;
getSortByPolulartyButton = null;

// location
getLocationButton = null;

// items 
getItems = function ()
{
    var items = {}
    var itemsLength = 0;
    var tops = document.querySelectorAll(".item");
    for (var i = 0; i < tops.length; i++)
    {
        var _top = tops[i];
        var img = _top.querySelector(".photo img");
        var a = _top.querySelector(".detail a.item-name");
        var id = /[?&]id=([^&]*)/.exec(a.href)[1];
        //var price = _top.querySelector(".cprice-area");
        var price = null;
        var shop = null;
        var _location = null;

        if (!a || !img)
            continue; // drop

        items[itemsLength++] = {
            extra: {
                top: _top
            },
            keyword: arg.itemName,
            name: a ? fixString(a.textContent) : "",
            id: id ? id : "",
            price: price ?fixPrice(fixString(price.textContent)) : "",
            shop: shop ? fixString(shop.textContent) : "",
            link: a ? a.href : "",
            imgsrc: https2http(img ? ((img.src && img.src) != "" ? img.src : ("http:" + img.getAttribute("data-src"))) : ""),
            location: _location ? fixString(_location.textContent) : ""
        };
    }
    log("# of tops : " + tops.length);
    log("# of items : " + itemsLength);
    return { items: items, length: itemsLength };
}

nextPageButton = function ()
{
    return document.querySelector("#mainsrp-pager > div > div > div > ul > li.item.next > a");
}